import jdk.swing.interop.SwingInterOpUtils;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        ArrayList<Account> accounts = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        while(true){
            menu1();
            int choice = scanner.nextInt();
            switch (choice){
                case 1:{
                    login(accounts,scanner);
                    break;
                }
                case 2:{
                    registered(accounts,scanner);
                    break;
                }
            }
        }
    }

    public static void menu1(){
        System.out.println("===========欢迎来到ATM机==========");
        System.out.println("1.登录账户");
        System.out.println("2.注册账户");
        System.out.println("================================");
        System.out.println("请选择你的操作");
    }

    public static void menu2(){
        System.out.println("========欢迎进入操作选择界面======");
        System.out.println("1.查询");
        System.out.println("2.存款");
        System.out.println("3.取款");
        System.out.println("4.转账");
        System.out.println("5.修改密码");
        System.out.println("6.退出");
        System.out.println("7.注销账户");
        System.out.println("==============================");
        System.out.println("请选择你的操作:");
    }

    public static void registered(ArrayList<Account> accounts,Scanner scanner){
        Random random = new Random();
        Account account = new Account();
        System.out.println("请输入用户名:");
        String name = scanner.next();
        account.setName(name);
        while(true){
            System.out.println("设置密码:");
            String password1 = scanner.next();
            System.out.println("再次确认密码:");
            String password2 = scanner.next();
            if(password1.equals(password2)){
                account.setPassword(password1);
                break;
            }else{
                System.out.println("两次密码不一致，请重新设置");
                continue;
            }
        }
        System.out.println("设定单次取款限额");
        double limit = scanner.nextDouble();
        account.setLimit(limit);
        while(true){
            String id = "";
            for(int i=0;i<8;i++){
                id += random.nextInt(10);
            }
            for(int i=0;i<accounts.size();i++){
                Account a = accounts.get(i);
                if(id.equals(a.getId())){
                    continue;
                }
            }
            account.setId(id);
            break;
        }
        System.out.println("恭喜你"+account.getName()+"先生/女士"+"你已开户成功"+"id号码是:"+account.getId());
        accounts.add(account);
    }

    public static void login(ArrayList<Account> accounts,Scanner scanner){
        if(accounts.size()==0){
            System.out.println("目前没有账户，先去注册一个再来吧");
            return;
        }
        int i;
        Account account = new Account();
        while(true){
            boolean loginsuccess = false;
            int flag = 0;
            System.out.println("输入id");
            String id = scanner.next();
            for(i=0;i<accounts.size();i++){
                Account a = accounts.get(i);
                if(a.getId().equals(id)){
                    account = accounts.get(i);//i代表了此时的账户位置
                    flag = 1;
                    break;
                }
            }
            if(flag==0){
                System.out.println("无此账户,请检查id");
                continue;
            }
            while(true){
                System.out.println("请输入密码");
                String password = scanner.next();
                if(account.getPassword().equals(password)){
                    loginsuccess = true;
                    break;
                }else{
                    System.out.println("密码错误");
                }
            }
            if(loginsuccess){
                System.out.println("登录成功");
                break;
            }
        }//登录过程
        while(true){
            boolean wantexit = false;
            menu2();
            int choice = scanner.nextInt();
            switch (choice){
                case 1:{
                    Inquire(accounts,scanner,i);
                    break;
                }
                case 2:{
                    Deposit(accounts,scanner,i);
                    break;
                }
                case 3:{
                    Withdrawal(accounts,scanner,i);
                    break;
                }
                case 4:{
                    Transfer(accounts,scanner,i);
                    break;
                }
                case 5:{
                    ChangeThePassword(accounts,scanner,i);
                    break;
                }
                case 6:{
                    wantexit = true;
                    break;
                }
                case 7:{
                    Revoked(accounts,scanner,i);
                    wantexit = true;
                    break;
                }
            }
            if(wantexit){
                break;
            }
        }
    }


    public static void Inquire(ArrayList<Account> accounts,Scanner scanner,int i){
        System.out.println("===========欢迎来到查询界面===========");
        Account account = accounts.get(i);
        System.out.println("可用余额为:"+account.getMoney());
    }

    public static void Deposit(ArrayList<Account> accounts,Scanner scanner,int i){
        System.out.println("============欢迎来到存款界面===========");
        Account account = accounts.get(i);
        double old = account.getMoney();
        System.out.println("请输入存款金额");
        double money = scanner.nextDouble();
        account.setMoney((old+money));
        accounts.set(i,account);
        System.out.println(money+"元存入成功");
    }

    public static void Withdrawal(ArrayList<Account> accounts,Scanner scanner,int i){
        System.out.println("============欢迎来到取款界面===========");
        Account account = accounts.get(i);
        double old = account.getMoney();
        System.out.println("目前的金额为"+old);
        while(true){
            System.out.println("输入你的取出的金额数");
            double out = scanner.nextDouble();
            if(out>account.getMoney()){
                System.out.println("余额不足");
                continue;
            }else{
                if(out>account.getLimit()){
                    System.out.println("超出单次取款限额");
                    continue;
                }else{
                    System.out.println("取款成功");
                    account.setMoney((old-out));
                    break;
                }
            }
        }
        accounts.set(i,account);
    }


    public static void Transfer(ArrayList<Account> accounts,Scanner scanner,int i){
        if(accounts.size()<2){
            System.out.println("暂时没有其他账户哦");
            return;
        }
        if(accounts.get(i).getMoney()==0){
            System.out.println("自己都没钱就别转啦");
            return;
        }
        int j;
        boolean find = false;
        System.out.println("============欢迎来到转账界面===========");
        Account account1 = accounts.get(i);
        double old1 = account1.getMoney();
        System.out.println("目前的金额为"+old1);
        System.out.println("请输入要转账的账户id:");
        String id = scanner.next();
        for(j=0;j<accounts.size();j++){
            if(accounts.get(j).getId().equals(id)){
                find = true;
                break;
            }
        }
        if(find==false){
            System.out.println("无此账户");
            return;
        }
        Account account2 = accounts.get(j);
        double old2 = account2.getMoney();
        while(true){
            System.out.println("输入你要转账的金额:");
            double money = scanner.nextDouble();
            if(money>old1){
                System.out.println("余额不足");
                continue;
            }else{
                System.out.println("转账成功");
                account1.setMoney(old1-money);
                account2.setMoney(old2+money);
                break;
            }
        }
        accounts.set(i,account1);
        accounts.set(j,account2);
    }


    public static void ChangeThePassword(ArrayList<Account> accounts,Scanner scanner,int i){
        Account account = accounts.get(i);
        String password;
        int times = 1;
        System.out.println("============欢迎来到密码修改界面===========");
        while(times<=3){
            System.out.println("请输入原密码:");
            password = scanner.next();
            if(account.getPassword().equals(password)){
                break;
            }else{
                System.out.println("密码错误，你还有"+(3-times)+"次机会");
                times++;
            }
            if(times>3){
                System.out.println("已经错误三次");
                return;
            }
        }
        String password1,password2;
        while(true){
            System.out.println("请输入要更改的密码:");
            password1 = scanner.next();
            System.out.println("再次确认:");
            password2 = scanner.next();
            if(password1.equals(password2)){
                break;
            }
        }
        System.out.println("修改成功");
        account.setPassword(password1);
        accounts.set(i,account);
    }


    public static void Revoked(ArrayList<Account> accounts,Scanner scanner,int i){
        System.out.println("确认要注销吗(若还有余额将全部清空)");
        System.out.println("输入\tyesyesyesyes\t来注销");
        String choice = scanner.next();
        if(choice.equals("yesyesyesyes")){
            accounts.remove(i);
            System.out.println("销户成功");
        }else{
            System.out.println("虚惊一场");
        }
    }
}
